package ywpt.web.action.knowledge;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.util.DateTools;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.KnowLib;
import ywpt.service.knowledge.KnowLibService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class KnowLibAction extends BaseAction implements ModelDriven<KnowLib>, Preparable{

	/**
	 * 知识库Action
	 */
	private static final long serialVersionUID = -4701208712509471788L;
	private static final int DEFAULT_PAGE_SIZE = 100;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private Long id;
	private KnowLib knowLib;
	private List<KnowLib> knowLibList = new ArrayList<KnowLib>();
	private KnowLibService knowLibService;
	
	//查询相关
	private String qTitle;
	private String qPublish;
	private Long qDepartId;
	private String qStartDate;
	private String qEndDate;
	
	public String getQTitle() {
		return qTitle;
	}
	public void setQTitle(String title) {
		qTitle = title;
	}
	public KnowLibService getKnowLibService() {
		return knowLibService;
	}
	public void setKnowLibService(KnowLibService knowLibService) {
		this.knowLibService = knowLibService;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public KnowLib getKnowLib() {
		return knowLib;
	}
	public void setKnowLib(KnowLib knowLib) {
		this.knowLib = knowLib;
	}
	public List<KnowLib> getKnowLibList() {
		return knowLibList;
	}
	public void setKnowLibList(List<KnowLib> knowLibList) {
		this.knowLibList = knowLibList;
	}
	public boolean[] getPopedom() {
		return popedom;
	}
	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	public String getQPublish() {
		return qPublish;
	}
	public void setQPublish(String publish) {
		qPublish = publish;
	}
 
	public Long getQDepartId() {
		return qDepartId;
	}
	public void setQDepartId(Long departId) {
		qDepartId = departId;
	}
	public String getQStartDate() {
		return qStartDate;
	}
	public void setQStartDate(String startDate) {
		qStartDate = startDate;
	}
	public String getQEndDate() {
		return qEndDate;
	}
	public void setQEndDate(String endDate) {
		qEndDate = endDate;
	}
	/*-------------------------------------------------------------------------------------------*/
	@Override
	public KnowLib getModel() {
		return this.knowLib;
	}
	@Override
	public void prepare() throws Exception {
		if(id!=null){
			 this.knowLib = this.knowLibService.getKnowLibById(id);
		}else{
			this.knowLib = new KnowLib();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.KNOW_LIB);
	}
	
	protected void getDataList(DetachedCriteria query ) {
		query.add(Restrictions.eq("checkFlag", true));
		query.add(Restrictions.eq("deleteFlag", false));
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows= knowLibService.getKnowLibCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),totalRows,
				DEFAULT_PAGE_SIZE,0);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("id"));
		query.setProjection(null);
		knowLibList.addAll(knowLibService.getKnowLibs(query, pageSize, rowStartEnd[0]));		
	}
	/*===========================================================================================*/
	public String list() throws Exception {
		this.knowLibList.clear();
		DetachedCriteria query = DetachedCriteria.forClass(KnowLib.class);
		getDataList(query);
		return SUCCESS;
	}
	
	public String search() throws Exception {
			DetachedCriteria query = DetachedCriteria.forClass(KnowLib.class);
			if (StringTools.isNullOrSpace(this.qTitle)) {
				query.add(Restrictions.or(Restrictions.like("title", "%"+this.qTitle+"%"), Restrictions.like("typeName", "%"+this.qTitle+"%")));
			}
			if (StringTools.isNullOrSpace(this.qPublish)) {
				query.createAlias("people", "people").add(Restrictions.like("people.peopleName", "%"+this.qPublish+"%"));
			}
			if (this.qDepartId!=null) {
				query.createAlias("depart", "depart").add(Restrictions.eq("depart.id", this.qDepartId));
			}
			if(!StringTools.isNullOrSpace(this.getQStartDate())&& !StringTools.isNullOrSpace(this.getQEndDate()))
			{				
				if(FORMAT.parse(this.getQStartDate()).before(FORMAT.parse(this.getQEndDate())))
					query.add(Restrictions.between("publDate",FORMAT.parse(this.getQStartDate()),DateTools.addDay(FORMAT.parse(this.getQEndDate()), 1)));
				else  
					query.add(Restrictions.between("publDate",FORMAT.parse(this.getQEndDate()),DateTools.addDay(FORMAT.parse(this.getQStartDate()),1)));
			}
			getDataList(query);
		return SUCCESS;
	}
	
	public String view()  {
		this.knowLib = this.knowLibService.getKnowLibById(id);
		return SUCCESS;
	}
}
